﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EHSubmit.Model;
using EHSubmit.Util;

namespace EHSubmit.Logic
{
    /// <summary>
    ///RestoresManager 的摘要说明
    /// </summary>
    public class RestoresManager
    {
        private const string SQL_INSERT_Restores = "INSERT INTO Restores(Role,ResAnnID,RestoreBody,RestoreUID,RestoreName,ResTime) values (@Role,@ResAnnID,@RestoreBody,@RestoreUID,@RestoreName,@ResTime)";
        private const string SQL_UPDATE_AnnByRes = "Update Announces SET rescnt = rescnt + 1,resLastTime =@resLastTime where AID =@AID ";
        private const string PARM_resLastTime = "@resLastTime";
        private const string PARM_RID = "@RID";
        private const string PARM_AID = "@AID";
        private const string PARM_Role = "@Role";
        private const string PARM_ResAnnID = "@ResAnnID";
        private const string PARM_RestoreBody = "@RestoreBody";
        private const string PARM_RestoreUID = "@RestoreUID";
        private const string PARM_RestoreName = "@RestoreName";
        private const string PARM_ResTime = "@ResTime";


        public RestoresManager() { }

        /// <summary>
        /// Restores信息被添加
        /// </summary>
        /// <param name="Announces">帖子回复对象</param>
        public void AddRestores(Restores restores)
        {
            try
            {
                int i;
                SqlParameter[] parms = new SqlParameter[6];
                parms[0] = new SqlParameter(PARM_Role, SqlDbType.NVarChar);
                parms[1] = new SqlParameter(PARM_ResAnnID, SqlDbType.Int);
                parms[2] = new SqlParameter(PARM_RestoreBody, SqlDbType.NVarChar, 400);
                parms[3] = new SqlParameter(PARM_RestoreUID, SqlDbType.NVarChar, 13);
                parms[4] = new SqlParameter(PARM_RestoreName, SqlDbType.NVarChar, 5);
                parms[5] = new SqlParameter(PARM_ResTime, SqlDbType.DateTime);
                parms[0].Value = restores.Role;
                parms[1].Value = restores.ResAnnID;
                parms[2].Value = restores.RestoreBody;
                parms[3].Value = restores.RestoreUID;
                parms[4].Value = restores.RestoreName;
                parms[5].Value = restores.ResTime;

                i = (int)SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_INSERT_Restores, parms);

            }
            catch
            { 
                //处理
            }
        }



        /// <summary>
        /// 获取回复的消息集合  
        /// </summary>
        /// <param name="pageIndex">当前页的页码</param>
        /// <returns>帖子消息集合</returns>
        public IList<Restores> GetRestores(int pageIndex, int annID)
        {
            PagedDataProvider pagedDataProvider = new PagedDataProvider();
            pagedDataProvider.TbName = "Restores";
            pagedDataProvider.FieldsToReturn = "*";
            pagedDataProvider.PageSize = 15;                //默认记录数为10
            pagedDataProvider.PageIndex = pageIndex;
            pagedDataProvider.DoCount = false;
            pagedDataProvider.IsDesc = true;
            pagedDataProvider.FieldToOrder = "ResTime";
            pagedDataProvider.Criteria = "ResAnnID = \'" + annID + "\'";

            SqlDataReader rdrRestores = pagedDataProvider.GetResults();
            IList<Restores> RestoresList = new List<Restores>();
            try
            {
                while (rdrRestores.Read())
                {
                    Restores restore = new Restores();

                    restore.RID = (Int32)rdrRestores["RID"];
                    restore.Role = rdrRestores["Role"] is DBNull ? string.Empty : (string)rdrRestores["Role"];
                    restore.RestoreBody = rdrRestores["RestoreBody"] is DBNull ? string.Empty : (string)rdrRestores["RestoreBody"];
                    restore.RestoreUID = rdrRestores["RestoreUID"] is DBNull ? string.Empty : (string)rdrRestores["RestoreUID"];
                    restore.RestoreName = rdrRestores["RestoreName"] is DBNull ? string.Empty : (string)rdrRestores["RestoreName"];
                    restore.ResAnnID = (Int32)rdrRestores["ResAnnID"];
                    restore.ResTime = rdrRestores["ResTime"] is DBNull ? DateTime.Now : (DateTime)rdrRestores["ResTime"];
                    RestoresList.Add(restore);
                }
            }
            finally
            {
                if (rdrRestores != null)   //关闭与数据库连接
                    rdrRestores.Close();
            }
            return RestoresList;
        }



        /// <summary>
        /// 获取回复总数量
        /// </summary>
        /// <returns>回复数量</returns>
        public int GetRestoresPageCount(int annID)
        {
            PagedDataProvider pagedDataProvider = new PagedDataProvider();
            pagedDataProvider.TbName = "Restores";
            pagedDataProvider.DoCount = true;
            pagedDataProvider.Criteria = "ResAnnID = \'" + annID + "\'";
            pagedDataProvider.TotalCount = pagedDataProvider.GetRecordCount();
            if (pagedDataProvider.TotalCount == -1)
            {
                //有待处理解决
                return -1;
            }
            else
            {
                return pagedDataProvider.TotalCount;
            }
        }

        public void UpdateAnn(DateTime resTime, int annid)
        {
            try
            {
                int i;
                SqlParameter[] parms = new SqlParameter[2];
                parms[0] = new SqlParameter(PARM_AID, SqlDbType.Int);
                parms[1] = new SqlParameter(PARM_resLastTime, SqlDbType.DateTime);
                parms[0].Value = annid;
                parms[1].Value = resTime;

                i = (int)SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_UPDATE_AnnByRes, parms);

            }
            catch
            {
                //处理
            }
        }
    }
}